package com.zeng.bean.restmodel;

import com.alibaba.fastjson.JSONObject;
import com.zeng.annotation.restmodel.RestModel;
import com.zeng.config.restmodel.RestTBCreateMode;
import com.zeng.pojo.restmodel.BaseRestModel;
import com.zeng.util.GenUtils;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * rest模型信息
 */
@Getter
@Slf4j
public class RestModelInfo {

    private final Class<? extends BaseRestModel> clazz;
    private final String modelName;
    private final String tableName;
    private final RestModel restModelAnnotation;
    private final BaseRestModel modelObj;

    private boolean use = true;
    private boolean useGet = true;
    private boolean usePost = true;
    private boolean usePut = true;
    private boolean useDelete = true;

    private RestTBCreateMode tbModel = RestTBCreateMode.AUTO;

    private RestModelInfo(Class<? extends BaseRestModel> clazz) throws InstantiationException, IllegalAccessException {
        this.clazz = clazz;
        this.modelName = clazz.getSimpleName();
        String tbName = clazz.getSimpleName();

        modelObj = clazz.newInstance();

        restModelAnnotation = clazz.getAnnotation(RestModel.class);

        if(restModelAnnotation != null) {

            use = restModelAnnotation.use();
            useGet = restModelAnnotation.useGet();
            usePost = restModelAnnotation.usePost();
            usePut = restModelAnnotation.usePut();
            useDelete = restModelAnnotation.useDelete();

            tbModel = restModelAnnotation.tbModel();

            if(!GenUtils.stringIsNullOrWhiteSpace(restModelAnnotation.tableName())){
                tbName = restModelAnnotation.tableName();
            }
        }

        this.tableName = tbName;
    }

    public static RestModelInfo load(Class<? extends BaseRestModel> clazz) throws InstantiationException, IllegalAccessException { return new RestModelInfo(clazz); }

    // region 生命周期方法

    public boolean onTableCreate(RestTableCreateParam param) {
        log.info("创建模型表：{}",param.getTableName());
        return modelObj.onTableCreate(param);
    }

    public boolean onTableDrop(RestTableInfo param){
        log.info("删除模型表：{}",param.getTableName());
        return modelObj.onTableDrop(param);
    }

    public boolean onSelect(RestQueryParam param) { return modelObj.onSelect(param); }
    public boolean onSelected(JSONObject object)  { return modelObj.onSelected(object); }
    public boolean onUpdate(RestQueryParam param) { return modelObj.onUpdate(param); }
    public boolean onDelete(RestQueryParam param) { return modelObj.onDelete(param); }
    public boolean onInsert(RestQueryParam param) { return modelObj.onInsert(param); }

    // endregion


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RestModelInfo that = (RestModelInfo) o;
        return use == that.use && useGet == that.useGet && usePost == that.usePost && usePut == that.usePut && useDelete == that.useDelete && Objects.equals(clazz, that.clazz) && Objects.equals(modelName, that.modelName) && Objects.equals(tableName, that.tableName) && Objects.equals(restModelAnnotation, that.restModelAnnotation) && Objects.equals(modelObj, that.modelObj);
    }

    @Override
    public int hashCode() {
        return Objects.hash(clazz, modelName, tableName, restModelAnnotation, modelObj, use, useGet, usePost, usePut, useDelete);
    }
}
